package screenpac.features;

import java.awt.Color;

import screenpac.model.GameStateInterface;
import screenpac.model.Node;
import screenpac.extract.Constants;

public class GaiRandomPill implements NodeScore, Constants {

    // this can be useful: make it public
    public Node randNode = null;

    public double score(GameStateInterface gs, Node node) {
        // most obvious way: iterate over the set of pills
        // int closest
    	boolean pillFound = false;
    	int tries = 10;
        int index = -1;
        
        while(!pillFound && (tries > 0)) {
        	index = rand.nextInt(gs.getMaze().getPills().size());
        	if(gs.getPills().get(gs.getMaze().getPills().get(index).pillIndex)) {
        		//pill is on
        		pillFound = true;
        		randNode = gs.getMaze().getPills().get(index);
        	}
        	tries--;
        }
        if(!pillFound) {
        	//If cannot be found randomly start from beginning
        	for(Node n : gs.getMaze().getPills()) {
        		if(gs.getPills().get(n.pillIndex)) {
        			randNode = n;
        			index = n.pillIndex;
        		}
        	}
        }
        
        if (randNode != null) {
            randNode.col = Color.green;
        }
        return index;
    }
}
